/**
 * 蓝牙插件常量定义
 */

// 平台类型
export const PLATFORMS = {
  H5: 'h5',
  MP_WEIXIN: 'mp-weixin',
  MP_ALIPAY: 'mp-alipay',
  MP_BAIDU: 'mp-baidu',
  MP_TOUTIAO: 'mp-toutiao',
  MP_QQ: 'mp-qq',
  MP_360: 'mp-360',
  APP_PLUS: 'app-plus'
}

// 蓝牙状态
export const BLUETOOTH_STATE = {
  UNKNOWN: 'unknown',
  RESETTING: 'resetting',
  UNSUPPORTED: 'unsupported',
  UNAUTHORIZED: 'unauthorized',
  POWERED_OFF: 'poweredOff',
  POWERED_ON: 'poweredOn'
}

// 连接状态
export const CONNECTION_STATE = {
  DISCONNECTED: 'disconnected',
  CONNECTING: 'connecting',
  CONNECTED: 'connected',
  DISCONNECTING: 'disconnecting'
}

// 错误码定义
export const ERROR_CODES = {
  // 通用错误
  OK: 0,
  FAIL: -1,
  
  // 蓝牙适配器错误
  BLUETOOTH_NOT_AVAILABLE: 10000,
  BLUETOOTH_NOT_ENABLED: 10001,
  BLUETOOTH_ALREADY_INIT: 10002,
  BLUETOOTH_NOT_INIT: 10003,
  
  // 设备发现错误
  DEVICE_NOT_FOUND: 10004,
  DISCOVERY_ALREADY_STARTED: 10005,
  DISCOVERY_NOT_STARTED: 10006,
  
  // 连接错误
  CONNECTION_FAIL: 10007,
  CONNECTION_TIMEOUT: 10008,
  ALREADY_CONNECTED: 10009,
  NOT_CONNECTED: 10010,
  
  // 服务和特征值错误
  SERVICE_NOT_FOUND: 10011,
  CHARACTERISTIC_NOT_FOUND: 10012,
  WRITE_NOT_PERMIT: 10013,
  READ_NOT_PERMIT: 10014,
  NOTIFY_NOT_PERMIT: 10015,
  
  // H5特有错误
  WEB_BLUETOOTH_NOT_SUPPORTED: 20001,
  USER_CANCELLED: 20002,
  PERMISSION_DENIED: 20003,
  DEVICE_UNAVAILABLE: 20004
}

// 错误消息
export const ERROR_MESSAGES = {
  [ERROR_CODES.BLUETOOTH_NOT_AVAILABLE]: 'Bluetooth adapter not available',
  [ERROR_CODES.BLUETOOTH_NOT_ENABLED]: 'Bluetooth adapter not enabled',
  [ERROR_CODES.BLUETOOTH_ALREADY_INIT]: 'Bluetooth adapter already initialized',
  [ERROR_CODES.BLUETOOTH_NOT_INIT]: 'Bluetooth adapter not initialized',
  [ERROR_CODES.DEVICE_NOT_FOUND]: 'Device not found',
  [ERROR_CODES.DISCOVERY_ALREADY_STARTED]: 'Device discovery already started',
  [ERROR_CODES.DISCOVERY_NOT_STARTED]: 'Device discovery not started',
  [ERROR_CODES.CONNECTION_FAIL]: 'Connection failed',
  [ERROR_CODES.CONNECTION_TIMEOUT]: 'Connection timeout',
  [ERROR_CODES.ALREADY_CONNECTED]: 'Device already connected',
  [ERROR_CODES.NOT_CONNECTED]: 'Device not connected',
  [ERROR_CODES.SERVICE_NOT_FOUND]: 'Service not found',
  [ERROR_CODES.CHARACTERISTIC_NOT_FOUND]: 'Characteristic not found',
  [ERROR_CODES.WRITE_NOT_PERMIT]: 'Write not permitted',
  [ERROR_CODES.READ_NOT_PERMIT]: 'Read not permitted',
  [ERROR_CODES.NOTIFY_NOT_PERMIT]: 'Notify not permitted',
  [ERROR_CODES.WEB_BLUETOOTH_NOT_SUPPORTED]: 'Web Bluetooth not supported',
  [ERROR_CODES.USER_CANCELLED]: 'User cancelled',
  [ERROR_CODES.PERMISSION_DENIED]: 'Permission denied',
  [ERROR_CODES.DEVICE_UNAVAILABLE]: 'Device unavailable'
}

// 特征值属性
export const CHARACTERISTIC_PROPERTIES = {
  READ: 'read',
  WRITE: 'write',
  WRITE_WITHOUT_RESPONSE: 'writeWithoutResponse',
  NOTIFY: 'notify',
  INDICATE: 'indicate'
}

// 默认配置
export const DEFAULT_CONFIG = {
  // 默认搜索时间（毫秒）
  DISCOVERY_TIMEOUT: 10000,
  // 默认连接超时时间（毫秒）
  CONNECTION_TIMEOUT: 10000,
  // 默认写入超时时间（毫秒）
  WRITE_TIMEOUT: 5000,
  // 默认读取超时时间（毫秒）
  READ_TIMEOUT: 5000
} 

/**
 * 常见BLE服务UUID映射
 * 帮助用户更容易地访问设备服务
 */
export const COMMON_BLE_SERVICES = {
  // 标准服务
  'battery_service': '0000180f-0000-1000-8000-00805f9b34fb',
  'device_information': '0000180a-0000-1000-8000-00805f9b34fb',
  'generic_access': '00001800-0000-1000-8000-00805f9b34fb',
  'generic_attribute': '00001801-0000-1000-8000-00805f9b34fb',
  'heart_rate': '0000180d-0000-1000-8000-00805f9b34fb',
  'blood_pressure': '00001810-0000-1000-8000-00805f9b34fb',
  'glucose': '00001808-0000-1000-8000-00805f9b34fb',
  'health_thermometer': '00001809-0000-1000-8000-00805f9b34fb',
  'human_interface_device': '00001812-0000-1000-8000-00805f9b34fb',
  'immediate_alert': '00001802-0000-1000-8000-00805f9b34fb',
  'link_loss': '00001803-0000-1000-8000-00805f9b34fb',
  'tx_power': '00001804-0000-1000-8000-00805f9b34fb',
  'current_time': '00001805-0000-1000-8000-00805f9b34fb',
  'reference_time_update': '00001806-0000-1000-8000-00805f9b34fb',
  'next_dst_change': '00001807-0000-1000-8000-00805f9b34fb',
  'cycling_power': '00001818-0000-1000-8000-00805f9b34fb',
  'cycling_speed_and_cadence': '00001816-0000-1000-8000-00805f9b34fb',
  'running_speed_and_cadence': '00001814-0000-1000-8000-00805f9b34fb',
  
  // 常见厂商服务
  'nordic_uart': '6e400001-b5a3-f393-e0a9-e50e24dcca9e', // Nordic UART Service
  'esp32_uart': '4fafc201-1fb5-459e-8fcc-c5c9c331914b', // ESP32 常用UART服务
  'xiaomi_miband': '0000fee0-0000-1000-8000-00805f9b34fb', // 小米手环
  'xiaomi_scale': '0000181b-0000-1000-8000-00805f9b34fb', // 小米体脂秤
}

/**
 * 设备类型对应的推荐服务
 */
export const DEVICE_TYPE_SERVICES = {
  'fitness': [
    'heart_rate', 'battery_service', 'device_information', 
    'cycling_power', 'running_speed_and_cadence'
  ],
  'health': [
    'heart_rate', 'blood_pressure', 'glucose', 'health_thermometer',
    'battery_service', 'device_information'
  ],
  'smart_watch': [
    'heart_rate', 'battery_service', 'device_information', 
    'current_time', 'immediate_alert'
  ],
  'iot_sensor': [
    'battery_service', 'device_information', 'generic_access',
    'environmental_sensing'
  ],
  'uart_device': [
    'nordic_uart', 'esp32_uart', 'battery_service', 'device_information'
  ],
  'xiaomi_device': [
    'xiaomi_miband', 'xiaomi_scale', 'battery_service', 'device_information'
  ],
  'generic': [
    'generic_access', 'generic_attribute', 'battery_service', 'device_information'
  ]
}

/**
 * 根据设备名称推测设备类型
 */
export const DEVICE_NAME_PATTERNS = {
  'fitness': ['band', 'fit', 'heart', 'pulse', 'hr'],
  'health': ['blood', 'glucose', 'thermometer', 'scale', 'health'],
  'smart_watch': ['watch', 'time', 'amazfit', 'apple watch'],
  'xiaomi_device': ['mi', 'xiaomi', 'miband', 'amazfit'],
  'uart_device': ['uart', 'serial', 'esp32', 'arduino', 'nordic'],
  'iot_sensor': ['sensor', 'temperature', 'humidity', 'light', 'motion']
} 